package com.demo.miaosha.Controller;

import com.demo.miaosha.Service.RedisService;
import com.demo.miaosha.Service.UserService;
import com.demo.miaosha.ToolUtil.ConstUtil;
import com.demo.miaosha.ToolUtil.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * DemoController
 * demo 示例
 * @blame 吴经纶
 */
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Resource
    UserService userService;
    @Resource
    RedisService redisService;
    @GetMapping("/hello")
    public ResponseMessage<String> hello() {
        String message = "hello world";
        return ResponseMessage.<String>builder()
                .code(ConstUtil.CodeConst.SUCCESS_CODE)
                .message(message).build();
    }

    @PutMapping("insertuser")
    public ResponseMessage<String> insertUser(@RequestParam String name) {

        userService.insertUser(name);
        redisService.set("newInsert",name);
        String message = "插入成功";
        return ResponseMessage.<String>builder()
                .code(ConstUtil.CodeConst.SUCCESS_CODE)
                .message(message).build();
    }
    @GetMapping("getuser")
    public ResponseMessage<String> getUser(@RequestParam String name) {

        String newInsert = redisService.get("newInsert",String.class);
        if (newInsert==null){
            newInsert = userService.findUser(name).getName();
        }
        String message = newInsert;
        return ResponseMessage.<String>builder()
                .code(ConstUtil.CodeConst.SUCCESS_CODE)
                .message(message).build();
    }


}
